import cv2
import numpy as np
import sys
from collections import Counter

def main():
    if len(sys.argv) < 2:
        print("用法: python select_liver_color.py <mask_image_path>")
        print("例如: python select_liver_color.py abdominal_US/AUS/annotations/train/ct2-1.png")
        sys.exit(1)

    path = sys.argv[1]

    # 读取mask图片
    mask = cv2.imread(path)
    if mask is None:
        print(f"❌ 无法读取图像文件: {path}")
        sys.exit(1)

    # 转换为RGB格式
    mask_rgb = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)

    # 展平后统计颜色
    colors = mask_rgb.reshape(-1, 3)
    counts = Counter(map(tuple, colors))

    print(f"\n颜色统计结果 (文件: {path})")
    print("格式: (R, G, B): 像素数")
    print("=" * 40)
    for color, count in counts.most_common():
        print(color, ":", count)

if __name__ == "__main__":
    main()
